package com.xish.api.activity;

import com.xish.api.activity.search.query.ActivitySearchSqlQuery;
import com.xish.model.bean.WzmActivity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class ActivityRepository {

    private static final Logger LOG = LoggerFactory.getLogger(ActivityRepository.class);

    @Autowired
    private ActivityMysqlDao mysqlDao;

    @Autowired
    private ActivityRedisDao redisDao;

    @Value("#{envFactory.get('activity.enableCache') ?: false}")
    private boolean enableCache;

    public List<Integer> searchIdListByQuery(ActivitySearchSqlQuery query) {
        return mysqlDao.searchIdListByQuery(query);
    }

    public long countByQuery(ActivitySearchSqlQuery query) {
        return mysqlDao.countByQuery(query);
    }

    public WzmActivity findById(int id) {
        WzmActivity activity = enableCache ? redisDao.findById(id) : null;

        if (activity == null) {
            activity = mysqlDao.findById(id);

            if ((activity != null) && enableCache) {
                redisDao.save(activity);
            }
        }

        return activity;
    }
}
